var koa = require('koa');
var app = new koa();
var Router = require('koa-router');
const koaBody = require('koa-body');
const koaStatic = require('koa-static')
const Logger = require('concurrency-logger').default;
let process = require('child_process');

var router = new Router();
const logger = new Logger({});

router.post('/do-deploy',(ctx, next)=>{
  if(ctx.request.body.password=='xxxxx'){ // webhook api 携带的请求口令
    process.exec('sh deploy.sh')
    ctx.body = {msg: 'deploy success'}
  }
})

router.get('/hello',(ctx, next)=>{  
  ctx.body = 'hello you !'
})
router.get('/',(ctx, next)=>{  
  ctx.body = 'deployer working ...'
})

let port = 10080

app
  .use(logger)
  .use(koaStatic('./static',{}))
  .use(koaBody())
  .use(router.routes())
  .use(router.allowedMethods())
  .listen(port);

  console.log(`server listen ${port}`);
